package com.ai.web.servlet;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Timer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import com.ai.web.controller.CommentRepairController;
import com.ai.web.schedule.WorkOrdersQueue;
import com.ai.web.schedule.WorkOrdersTimerTask;
import com.ai.web.service.RepairService;
import com.ai.web.util.WorkStatus;
import com.ai.web.vo.RepairV;

public class WorkOrderServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8604799751229069519L;
	
	private static Logger logger = Logger.getLogger(CommentRepairController.class);
	//时间间隔
	private static final long PERIOD_DAY = 10 * 1000;

	@Autowired
    private RepairService repairService;
	
	public WorkOrderServlet() {
		WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
		repairService = (RepairService)context.getBean("repairService");
	}
	
	@Override
	public void init() throws ServletException {
		try {
			List<RepairV> list = repairService.getListByStatus(WorkStatus.NEW.getValue());
			for(RepairV repairV:list) {
				WorkOrdersQueue.offer(repairV);
			}
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			logger.info("WorkOrderServlet......................................."+sdf.format(new Date()));	
			//启动工单自动下发定时任务
			Timer timer = new Timer();
			WorkOrdersTimerTask task = new WorkOrdersTimerTask();
			Calendar calendar = Calendar.getInstance();
			Date date=calendar.getTime(); 
			timer.scheduleAtFixedRate(task,date,PERIOD_DAY);
		}catch(Exception ex) {
			ex.printStackTrace();
		}
		
	
	}

}
